#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 100;
const ll linf = 1e18 + 100;
const int N = 1e5;
template <typename T>
bool umx(T &a, T b) {
return (b > a) ? (a = b, 1) : 0;
}
template <typename T>
bool umn(T &a, T b) {
return (b < a) ? (a = b, 1) : 0;
}
vector <vector <pair<int, int>>> g;
vector <map<int, int>> g1;
vector <int> met;
void dfs_build(int v) {
met[v] = 1;
for (auto [u, w] : g[v]) {
if (u == v) continue;
if (!met[u]) {
dfs_build(u);
}
for (auto [u1, w1] : g[u]) {
if (u1 == v) continue;
int cost = (w1 + w) * (w1 + w);
if (g1[v].find(u1) == g1[v].end()) {
g1[v][u1] = cost;
g1[u1][v] = cost;
} else {
umn(g1[v][u1], cost);
umn(g1[u1][v], cost);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int n, m;
cin >> n >> m;
g.resize(n + 1), g1.resize(n + 1), met.resize(n + 1);
for (int i = 0; i < m; i++) {
int x, y, w;
cin >> x >> y >> w;
g[x].push_back({y, w});
g[y].push_back({x, w});
}
vector <int> len(n + 1, inf);
len[1] = 0;
set <pair<int, pair<int, int>>> t;
t.insert({0, {0, 1}});
for (int i = 2; i <= n; i++) {
t.insert({inf, {0, i}});
}
while((int)t.size() > 0) {
auto x = *t.begin();
t.erase(x);
// cout << x.first << " " << x.second.first << " " << x.second.second << "\n";
for (auto [u, w] : g[x.second.second]) {
if (x.second.first > 0) {
int cost = x.first + (x.second.first + w) * (x.second.first + w);
if (len[u] > cost) {
t.erase({len[u], {0, u}});
len[u] = cost;
t.insert({len[u], {0, u}});
}
} else {
t.insert({x.first, {w, u}});
}
}
}
for (int i = 1; i <= n; i++) {
if (len[i] == inf) cout << "-1 ";
else cout << len[i] << " ";
}
}
461. Hamming Distance | 1748. Sum of Unique Elements |
897. Increasing Order Search Tree | 905. Sort Array By Parity |
1351. Count Negative Numbers in a Sorted Matrix | 617. Merge Two Binary Trees |
1450. Number of Students Doing Homework at a Given Time | 700. Search in a Binary Search Tree |
590. N-ary Tree Postorder Traversal | 589. N-ary Tree Preorder Traversal |
1299. Replace Elements with Greatest Element on Right Side | 1768. Merge Strings Alternately |
561. Array Partition I | 1374. Generate a String With Characters That Have Odd Counts |
1822. Sign of the Product of an Array | 1464. Maximum Product of Two Elements in an Array |
1323. Maximum 69 Number | 832. Flipping an Image |
1295. Find Numbers with Even Number of Digits | 1704. Determine if String Halves Are Alike |
1732. Find the Highest Altitude | 709. To Lower Case |
1688. Count of Matches in Tournament | 1684. Count the Number of Consistent Strings |
1588. Sum of All Odd Length Subarrays | 1662. Check If Two String Arrays are Equivalent |
1832. Check if the Sentence Is Pangram | 1678. Goal Parser Interpretation |
1389. Create Target Array in the Given Order | 1313. Decompress Run-Length Encoded List |